/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.unittests.service.services.triggers;

import opiam.admin.faare.PropertiesManager;
import opiam.admin.faare.exception.ServiceException;
import opiam.admin.faare.service.UserContext;
import opiam.admin.faare.service.services.StandardService;
import opiam.admin.faare.service.services.triggers.TriggersService;
import opiam.admin.faare.unittests.TestResult;
import opiam.admin.faare.unittests.TestUtils;
import opiam.admin.faare.unittests.javabeans.PersonTest;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import java.util.ArrayList;


/**
 * Testing the mailSenderTrigger plugin.
 */
public class MailSenderTriggerTest extends TestCase
{
    /** Instance of logger. */
    private static Logger _logger = Logger.getLogger(MailSenderTriggerTest.class);

    /** Instance of the trace class. */
    private static TestUtils _test = new TestUtils(_logger);

    /** Test user DN with profile by default. */
    private static final String DN_USER = "uid=jreuter, ou=People, dc=mycompany,dc=com";

    /** Test user login. */
    private static final String LOGIN_USER = "jreuter"; //uid

    /** Test user password. */
    private static final String PASSWORD_USER = "destroy";

    /** User context. */
    private static UserContext _USERCONTEXT = new UserContext();

    /** Test user DN with limited profile ("PHunt"). */
    private static final String DN_USER_LP = "uid=phunt, ou=People, dc=mycompany,dc=com";

    /** Test user login. */
    private static final String LOGIN_USER_LP = "phunt"; //uid

    /** Test user password. */
    private static final String PASSWORD_USER_LP = "wastewater";

    /** User context. */
    private static UserContext _USERCONTEXTLP = new UserContext();

    /** Test user DN with profile "HR_managers". */
    private static final String DN_USER_HR = "uid=kvaughan,ou=People,dc=mycompany,dc=com";

    /** Test user login. */
    private static final String LOGIN_USER_HR = "kvaughan"; //uid

    /** Test user password. */
    private static final String PASSWORD_USER_HR = "bribery";

    /** User context. */
    private static UserContext _USERCONTEXTHR = new UserContext();

    /** Mail recipient. */
    private static final String RECIPIENT_MAIL_ADDRESS = "person_test@mycompany.com";
    //private static final String RECIPIENT_MAIL_ADDRESS = "joe.baf@opiam.org";

    /**
     * Creates a new MailSenderTriggerTest object.
     *
     * @param arg0 Parameter.
     */
    public MailSenderTriggerTest(String arg0)
    {
        super(arg0);

        try
        {
            PropertiesManager.getInstance();

            if (!_USERCONTEXT.isLoggedIn())
            {
                StandardService.logon(LOGIN_USER, PASSWORD_USER, _USERCONTEXT);
            }

            if (!_USERCONTEXTLP.isLoggedIn())
            {
                StandardService.logon(LOGIN_USER_LP, PASSWORD_USER_LP,
                    _USERCONTEXTLP);
            }

            if (!_USERCONTEXTHR.isLoggedIn())
            {
                StandardService.logon(LOGIN_USER_HR, PASSWORD_USER_HR,
                    _USERCONTEXTHR);
            }
        }
        catch (Exception e)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("MailSenderTriggerTest constructor error");
            msg.append(TestUtils.NEXT);
            msg.append(e.toString());
            _test.displayResultError(msg.toString());
        }
    }

    /**
     * Method of the tests suite.
     *
     * @return Tests result.
     */
    public static Test suite()
    {
        TestSuite suite = new TestSuite();

        suite.addTest(new MailSenderTriggerTest("testBegin"));

        suite.addTest(new MailSenderTriggerTest("testExecute"));

        suite.addTest(new MailSenderTriggerTest("testEnd"));

        return suite;
    }

    /**
     * Main method of the test class.
     *
     * @param args  Sets of the parameters.
     */
    public static void main(String[] args)
    {
        junit.textui.TestRunner.run(suite());
    }

    /**
     * Method to indicate the beginning of the class test.
     *
     */
    public static void testBegin()
    {
        //  To initialize the tests statement
        TestResult.getInstance().setInitiator("MailSenderTriggerTest");

        _test.displayHeaderClass("MailSenderTriggerTest");
        assertTrue(true);
    }

    /**
     * Method to indicate the end of the class test.
     *
     */
    public static void testEnd()
    {
        _test.displayFooterClass(null);
        assertTrue(true);

        // To display the tests statement
        _test.displayTestStatement("MailSenderTriggerTest");
    }

    /**
     * Test the execution of the MailSenderTrigger.
     */
    public void testExecute()
    {
        _test.displayHeaderMethod("testExecute");

        try
        {
            PersonTest entry = new PersonTest();
            entry.setDn("uid=test, ou=People, dc=mycompany,dc=com");
            ArrayList mails = new ArrayList();
            mails.add(RECIPIENT_MAIL_ADDRESS);
            entry.setMail(mails);

            _test.displayMsg("create with profile by default");

            try
            {
                TriggersService.launchPreOp("CREATE", entry, _USERCONTEXT);
                _test.displayResultSuccess(
                    "PREOP: The emails are on the way...");
            }
            catch (ServiceException e)
            {
                _test.displayResultFailure(e.toString());
            }

            try
            {
                TriggersService.launchPostOp("CREATE", entry, _USERCONTEXT);
                _test.displayResultSuccess(
                    "POSOP: The emails are on the way...");
            }
            catch (ServiceException e)
            {
                _test.displayResultFailure(e.toString());
            }

            _test.displayMsg("create with profile JHunt");

            try
            {
                TriggersService.launchPreOp("CREATE", entry, _USERCONTEXTLP);
                _test.displayResultSuccess(
                    "PREOP: The emails are on the way...");
            }
            catch (ServiceException e)
            {
                _test.displayResultFailure(e.toString());
            }

            try
            {
                TriggersService.launchPostOp("CREATE", entry, _USERCONTEXTLP);
                _test.displayResultSuccess(
                    "POSOP: The emails are on the way...");
            }
            catch (ServiceException e)
            {
                _test.displayResultFailure(e.toString());
            }

            _test.displayMsg("create with profile HR_managers");

            try
            {
                TriggersService.launchPreOp("CREATE", entry, _USERCONTEXTHR);
                _test.displayResultSuccess(
                    "PREOP: The emails are on the way...");
            }
            catch (ServiceException e)
            {
                _test.displayResultFailure(e.toString());
            }

            try
            {
                TriggersService.launchPostOp("CREATE", entry, _USERCONTEXTHR);
                _test.displayResultSuccess(
                    "POSOP: The emails are on the way...");
            }
            catch (ServiceException e)
            {
                _test.displayResultFailure(e.toString());
            }
        }
        catch (Exception ex)
        {
            StringBuffer msg = new StringBuffer();
            msg.append("Exception : ");
            msg.append(TestUtils.NEXT);
            msg.append(ex.toString());
            _test.displayResultError(msg.toString());
        }

        _test.displayFooterMethod("testExecute");
    }
}
